
var client
var mqtt_st = false
var tbody = document.getElementById("tbody")
var rows = tbody.getElementsByTagName("tr")
function cmp(front, now) {
  console.log("cmp")
  var f_row = rows[front].getElementsByTagName("td")[2]
  var n_row = rows[now].getElementsByTagName("td")[2]
  var f_vale = 0
  var n_vale = 0
  switch (f_row.innerHTML) {
    case "打开":
      f_vale = 100
      break
    case "关闭":
      f_vale = 200
      break
    case "离线":
      f_vale = 0
      break
  }
  switch (n_row.innerHTML) {
    case "打开":
      n_vale = 100
      break
    case "关闭":
      n_vale = 200
      break
    case "离线":
      n_vale = 0
      break
  }
  if (f_vale > n_vale) {
    return true
  } else {
    return false
  }
}
function sortTable() {
  var flag = true;
  while (flag) {
    flag = false;
    for (var i = 1; i < rows.length; i++) {
      if (cmp(i - 1, i)) {
        var temp = rows[i - 1].cloneNode(true);
        tbody.replaceChild(rows[i].cloneNode(true), rows[i - 1]);
        tbody.replaceChild(temp, rows[i]);
        rows = tbody.getElementsByTagName("tr")
        flag = true;
      }
    }
  }
}
var sortT=document.getElementById("sort")
sortT.addEventListener("click", sortTable)
function connect() {
  if (client != undefined) {
    client.end()
    mqtt_st = false
    document.getElementById("mqtt_st").innerHTML = "未连接"
    document.getElementById("mqtt_st").style.backgroundColor = "crimson";
  }
  var url = "wss://" + window.location.hostname + ":1886/";
  var usern = "wlw";
  var passswd = "wlw";
  clientId = `mqtt_${Math.random().toString(16).slice(3)}`;
  var options = {
    clientId: clientId,
    clean: true,
    connectTimeout: 1000,
    reconnectPeriod: 250,
    username: usern,
    password: passswd
  }
  client = mqtt.connect(url, options)
  console.log(url)
  console.log(options)
  client.on('error', function (err) {
    console.error('Connection error:' + err);
    alert('连接失败');
    client.end();
    mqtt_st = false
    var style = document.createElement("style")
    style.innerHTML = `
        .mqtt-status span::after {
            content: "未连接";
            background-color: crimson;
            /* 鲜绿色背景 */
            color: #fff;
            padding: 5px 10px;
            border-radius: 5px;
            margin-left: 10px;
            white-space: nowrap;
        }`
    document.head.appendChild(style)
  })
  client.on('connect', function (packet) {

    var style = document.createElement("style")
    style.innerHTML = `
        .mqtt-status span::after {
            content: "已连接";
            background-color:  #28a745;
            /* 鲜绿色背景 */
            color: #fff;
            padding: 5px 10px;
            border-radius: 5px;
            margin-left: 10px;
            white-space: nowrap;
        }`
    document.head.appendChild(style)
    mqtt_st = true
  })
  client.subscribe('win_st_re', function (err) {
    if (err) {
      alert("订阅win_st_re主题失败")
    }
  })
  client.on("message", function (topic, payload) {
    console.log(payload.toString().slice())
    document.getElementById(payload.toString().split(" ")[1]).innerHTML = (payload.toString().split(" ")[0] == "1" ? "打开" : "关闭")
    document.getElementById(payload.toString().split(" ")[1]).style.backgroundColor = (payload.toString().split(" ")[0] == "0" ? "forestgreen" : "yellow")
  })
}

function sendq() {
  client.unsubscribe("win_st_re")
  client.subscribe('win_st_re', function (err) {
    if (err) {
      alert("订阅失败")
    }
  })
  if (!mqtt_st) {
    alert("未连接mqtt服务器")
    return
  }
  client.publish("win_st", 'query', { qos: 2, retain: false }, function (error) {
    if (error) {
      alert("查询失败请重新查询")
    } else {
      var table = document.getElementById("table")
      var tbody = document.getElementsByTagName("tbody")
      var rows = tbody[0].getElementsByTagName("tr");

      for (var i = 0; i < rows.length; i++) {
        // 获取当前行的所有单元格
        var cells = rows[i].getElementsByTagName("td");
        cells[2].style.backgroundColor = "rgba(168, 168, 168, 0.5)";
        cells[2].innerHTML = "离线"
      }
    }
  })
}
var sbu = document.getElementById("search_button")
sbu.addEventListener("click", sendq)